#ifndef FNDEFER_H
#define FNDEFER_H

#include <functional>
typedef std::function<void()> fnDefer;

class Defer
{
public:
    Defer(fnDefer fn) : m_fn(fn)
    {
    }
    ~Defer()
    {
        if (m_fn)
            m_fn();
    }

private:
    fnDefer m_fn;
};

#define defer1(a,b) a##b
#define defer2(a, b) defer1(a, b)
#define defer(expr) Defer defer2(__Defer__,__COUNTER__) ([&](){expr;}) 
// ==>
// auto defer = [&](){ fclose(fp); };
// defer();
// 




#endif
